package m.store.model;

import m.base.model.StoreInfo;
import m.base.model.UserInfo;
import m.common.model.*;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import manage.model.StatusModel;

import java.util.List;
import java.util.stream.Collectors;

@TableMeta(name="ts_repository_info",description="仓库信息")
public class RepositoryInfo extends StatusModel implements DictionaryModel, MigrationModel {
    @Override
    public List<String> migrationOidList(String storeOid) throws Exception {
        return ModelQueryList.getModelList(this.getClass(),new String[]{"oid"},null,
                        QueryCondition.eq("store.oid",storeOid))
                .stream().map(Model::getOid).collect(Collectors.toList());
    }
    @LinkTableMeta(name="store_oid",table= StoreInfo.class,description="所属商户")
    private StoreInfo store;
    @FieldMeta(name="name",type= FieldType.STRING,length=50,description="名称")
    private String name;
    @FieldMeta(name="sort",type= FieldType.INT,description="排序")
    private Integer sort;
    @LinkTableMeta(name="manage_user_oid",table= UserInfo.class,description="管理用户")
    private UserInfo manageUser;
    @FieldMeta(name="def_repository",type= FieldType.STRING,description="默认仓库",dictOption = "Y:是,N:否")
    private String defRepository;
    public final static String Yes="Y";
    public final static String No="N";

    public String getDefRepository() {
        return defRepository;
    }

    public void setDefRepository(String defRepository) {
        this.defRepository = defRepository;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public StoreInfo getStore() {
        return store;
    }

    public void setStore(StoreInfo store) {
        this.store = store;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UserInfo getManageUser() {
        return manageUser;
    }

    public void setManageUser(UserInfo manageUser) {
        this.manageUser = manageUser;
    }

    @Override
    public String getDictValue() {
        return getOid();
    }

    @Override
    public String getDictName() {
        return getName();
    }

    @Override
    public String getDictParentOid() {
        return "";
    }

    @Override
    public String getDictStatus() {
        return "0".equals(getStatus())?"Y":"N";
    }

    @Override
    public List<RepositoryInfo> getCacheList(String storeOid) throws Exception {
        return ModelQueryList.getModelList(RepositoryInfo.class,new String[]{"oid","name","defRepository","status"},null,
                QueryCondition.eq("store.oid",storeOid), QueryOrder.asc("sort"));
    }
}
